﻿using System;
using System.Text;
using _5E.Utils.EnumHelper.Extension;

namespace _5E.Utils.CheckHelper
{
    /// <summary>
    /// 程序说明：公用验证
    /// 创建作者：ahl
    /// 创建时间：2012-04-02
    /// </summary>
    public class CommonVerification : IVerification
    {
        #region 变量

        private StringBuilder m_Rule = new StringBuilder();

        private string m_Value = string.Empty;

        private StringBuilder m_DefaultErrorMessage = new StringBuilder();

        private string m_CustomErrorMessage = null;

        private bool m_Result = true;

        private ErrorMessageMode m_Mode = ErrorMessageMode.Default;

        #endregion

        #region 属性

        public string ErrorMessage
        {
            get
            {
                return this.m_Result ? string.Empty : (this.m_Mode == ErrorMessageMode.Default ?
                    this.m_DefaultErrorMessage.ToString() : this.m_CustomErrorMessage);
            }
        }

        #endregion

        #region 构造函数

        public CommonVerification(string name, VerificationType type, string value)
        {
            this.m_Value = value;
            VerificationAttribute attribute;
            if (type.TryGetAttribute(out attribute))
            {
                this.m_DefaultErrorMessage.AppendFormat("{0}要为{1}", name, attribute.Type);
                this.m_Rule.AppendFormat("^{0}", attribute.Rule);
            }
        }

        #endregion

        #region 实例方法

        public bool Validate()
        {
            var regVerification = new RegexVerification(this.m_Rule.Append("$").ToString(), this.m_Value, string.Empty);
            this.m_Result = regVerification.Validate();
            return this.m_Result;
        }

        public void AddFixedLength(Int32 fixedLength)
        {
            this.m_Rule.Append("{").Append(fixedLength).Append("}");
            this.m_DefaultErrorMessage.AppendFormat("且长度为{0}!", fixedLength);
        }

        public void AddLength(Int32 minLength, Int32 maxLength)
        {
            this.CheckLength(minLength, maxLength);
            this.SetErrorMessage(minLength, maxLength);
            this.m_Rule.Append("{").AppendFormat("{0},", minLength);
            if (0 < maxLength)
            {
                this.m_Rule.Append(maxLength);
            }
            this.m_Rule.Append("}");
        }

        public void SetErrorMessage(string errorMessage)
        {
            if (!string.IsNullOrEmpty(errorMessage))
            {
                this.m_CustomErrorMessage = errorMessage;
                this.m_Mode = ErrorMessageMode.Custom;
            }
        }

        private void CheckLength(Int32 minLength, Int32 maxLength)
        {
            if (minLength == 0 && maxLength == 0)
            {
                throw new ArgumentException("最小长度限制以及最大长度限制不可同时为0!");
            }
            if (maxLength <= minLength)
            {
                throw new ArgumentException("最大长度限制必须比最小长度限制大!");
            }
        }

        private void SetErrorMessage(Int32 minLength, Int32 maxLength)
        {
            if (minLength == 0)
            {
                this.m_DefaultErrorMessage.AppendFormat("且长度不大于{0}!", maxLength);
            }
            else if (maxLength == 0)
            {
                this.m_DefaultErrorMessage.AppendFormat("且长度不小于{0}!", minLength);
            }
            else
            {
                this.m_DefaultErrorMessage.AppendFormat("且长度为{0}-{1}!", minLength, maxLength);
            }
        }

        #endregion
    }
}
